<template>
  <NModal :show="state.dialogFormVisible" @onUpdateShow="close">
    <div ref="modalRef" class="edit-dia" style="width: 800px">
      <MacHeader
        title="选择用户"
        :is-fullscreen="isFullscreen"
        :show-desc="false"
        @toggle="toggle"
        @close="close"
      />
      <div class="content">
        <div class="search">
          <NForm
            inline
            :model="tableInfo.queryForm"
            label-width="auto"
            label-placement="left"
          >
            <NFormItem label="昵称">
              <NInput
                v-model:value="tableInfo.queryForm.nickname"
                placeholder="请输入昵称"
                clearable
                style="width: 200px"
              />
            </NFormItem>
            <NFormItem label="邮箱">
              <NInput
                v-model:value="tableInfo.queryForm.email"
                placeholder="请输入邮箱"
                clearable
                style="width: 200px"
              />
            </NFormItem>
            <NFormItem>
              <NButton type="primary" @click="queryData">
                <template #icon>
                  <n-icon>
                    <i class="esayIcon esayIcon-sousuo"></i>
                  </n-icon>
                </template>
                <span>搜索</span>
              </NButton>
            </NFormItem>
          </NForm>
        </div>
        <NDataTable
          style="height: 500px"
          :columns="columns"
          :loading="tableInfo.listLoading"
          :data="tableInfo.tableData"
        />
        <div class="pagination mt20">
          <NPagination
            v-model:page="tableInfo.queryForm.pageNo"
            :page-count="tableInfo.total"
            :page-sizes="tableInfo.pageSizes"
            show-quick-jumper
            show-size-picker
            @updatePage="pageChange"
            @updatePageSize="sizeChange"
          />
        </div>
      </div>
    </div>
  </NModal>
</template>
<script setup lang="ts">
import { reactive, ref, h, toRaw } from 'vue'
import { useFullscreen } from '@vueuse/core'
import {
  NModal,
  NInput,
  NDataTable,
  NPagination,
  NAvatar,
  NButton,
  NForm,
  NFormItem,
  NIcon
} from 'naive-ui'
import type { DataTableColumns } from 'naive-ui'
import MacHeader from '@/components/codeBlock/MacHeader'
import { useApi } from '@/hooks/useApi'
import userApi from '@/service/api/operation/user'
/**
 *  弹框
 * */
const modalRef = ref()
const { isFullscreen, toggle } = useFullscreen(modalRef)
const state = reactive({
  dialogFormVisible: false
})
const open = () => {
  tableInfo.queryForm.nickname = ''
  tableInfo.queryForm.email = ''
  queryData()
  state.dialogFormVisible = true
}
const close = () => {
  state.dialogFormVisible = false
}
defineExpose({
  open
})

/**
 *  列表
 * */
const { pageChange, sizeChange, tableInfo, queryData } = useApi({
  initForm: {
    nickname: '',
    email: ''
  },
  isRequest: false,
  getData: userApi.list
})
const columns: DataTableColumns<any> = [
  {
    title: '昵称',
    key: 'nickname'
  },
  {
    title: '头像',
    key: 'avatar',
    align: 'center',
    render(row: any) {
      return h(NAvatar, {
        src: row.avatar,
        size: 'medium',
        round: true
      })
    }
  },
  {
    title: '操作',
    key: 'total',
    align: 'center',
    width: 300,
    render(row: any) {
      return h('div', [
        h(
          NButton,
          {
            size: 'small',
            type: 'warning',
            class: 'ml10',
            onClick: () => handChoose(row)
          },
          { default: () => '选择' }
        )
      ])
    }
  }
]
const emit = defineEmits(['change'])
const handChoose = (row: any) => {
  emit('change', toRaw(row))
  close()
}
</script>

<style scoped lang="scss"></style>
